<?php
namespace app\admin\controller\article;
use think\facade\Route as Url;
use app\admin\controller\AuthController;
use app\admin\model\system\SystemAttachment;
use app\admin\model\article\{ArticleCategory as ArticleCategoryModel,Article as ArticleModel};
use services\{FormBuilder as Form,UtilService as Util,JsonService as Json};
use think\facade\Request;


/**
 * 文章分类管理  控制器
 * */
class ArticleCategory extends AuthController
{

    /**
     * 分类显示
     * */
    public function index()
    {
        if (Request::isAjax()) {
            $where = Util::getMore([
                ['page', 1],
                ['limit', 20],
                ['title',''],
            ]);
            return Json::successlayui(ArticleCategoryModel::getList($where));
        }
        return $this->fetch();
    }

    /**
     * @name: 表单
     * @Author: Sam
     * @Date: 2020-10-19 11:28:02
     * @msg: 
     * @param {type} 
     * @return {type} 
     */
    public function form(){
        if($id = Request::param('id/d','')){
            $article = ArticleCategoryModel::get($id);
            if(!$article)  return Json::fail('数据不存在!');
            $detail = $article->getData();
        }else{
            $detail = [];
        }
  
        $f = array();
        $f[] = Form::select('pid', '父级id',$detail?(string)$detail['pid']:0)->setOptions(function () {
            $list = ArticleCategoryModel::getTierList();
            $menus[] = ['value' => 0, 'label' => '顶级分类'];
            foreach ($list as $menu) {
                $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['title']];
            }
            return $menus;
        })->filterable(1);
        $f[] = Form::input('title', '分类名称',$detail?$detail['title']:'');
        $f[] = Form::input('intr', '分类简介',$detail?$detail['intr']:'')->type('textarea');
        $f[] = Form::frameImageOne('image', '分类图片', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')),$detail?$detail['image']:'')->icon('image')->width('100%')->height('500px');
        $f[] = Form::number('sort', '排序', $detail?$detail['sort']:0);
        $f[] = Form::radio('status', '状态', $detail?$detail['status']:1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
        $form = Form::make_post_form('添加分类', $f, Url::buildUrl('save',['id'=>$id]));
        $this->assign(compact('form'));
        return $this->fetch('public/form-builder');
    }
 


        /**
     * 快速编辑
     *
     * @return json
     */
    public function set_value($field = '', $id = '', $value = '')
    {
        $field == '' || $id == '' || $value == '' && JsonService::fail('缺少参数');
        if (ArticleCategoryModel::where(['id' => $id])->update([$field => $value])) {
            return Json::successful('保存成功');
        } else {
            return Json::fail('保存失败');
        }

    }

    /**
     * 保存分类管理
     * */
    public function save()
    {
        $data       = Request::post();
        $data['id'] = Request::param('id/d','');
        if($data['id']){
            $res = ArticleCategoryModel::update($data);
        }else{
            $res = ArticleCategoryModel::create($data);
        }
        if($res){
             return Json::successful('保存成功');
        }else{
             return Json::fail('保存失败');
        }
    }

            /**
     * @name: 删除
     * @Author: Sam
     * @Date: 2020-05-21 16:37:42
     * @msg:
     * @param {type}
     * @return:
     */
    public function del()
    {
        $id = Request::param('id/a'); //转化数组
        !$id && JsonService::fail('缺少参数');
        if (!ArticleCategoryModel::delArticleCategory($id)) {
            return Json::fail(ArticleCategoryModel::getErrorInfo());
        } else {
            return Json::success('删除成功');
        }

    }




}

